﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Runtime.Serialization;

namespace ARR.Services.ServiceModel
{
    class ServiceErrorHandler : IErrorHandler
    {
        public bool HandleError(Exception error)
        {
            if (error is FaultException)
                return false;

            //TODO: Agregar en este punto el logging de la excepción.
            return true;
        }

        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            if (error is FaultException)
                return;

            StringBuilder sb = new StringBuilder();
            sb.AppendLine(error.Message);
            sb.AppendLine();
            sb.AppendLine(error.StackTrace);

            MessageFault faultMessage = MessageFault.CreateFault(
                new FaultCode("Sender"),
                new FaultReason(sb.ToString()),
                error, // This will go into the detail property
                new NetDataContractSerializer());

            fault = Message.CreateMessage(version, faultMessage, null);
        }
    }
}
